package com.zeng.controller;

import com.zeng.entity.Audit;
import com.zeng.entity.Logsinfo;
import com.zeng.entity.ResultPage;
import com.zeng.entity.Userinfo;
import com.zeng.service.AuditService;
import com.zeng.service.LeagueTableQueryService;
import com.zeng.utils.getUserIp;
import com.zeng.service.LogsinfoService;
import com.zeng.service.UserinfoService;
import com.zeng.utils.TimeUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * (Audit)表控制层
 *
 * @author makejava
 * @since 2021-03-19 10:49:18
 */
@RestController
@RequestMapping("audit")
public class AuditController {
    /**
     * 服务对象
     */
    @Resource
    private AuditService auditService;
    @Resource
    private UserinfoService userinfoService;
    @Resource
    private LogsinfoService logsinfoService;
    @Resource
    private LeagueTableQueryService leagueTableQueryService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Audit selectOne(Integer id) {
        return this.auditService.queryById(id);
    }

    /**
     * 管理员通过申请
     *
     * @param id
     * @return
     */
    @GetMapping("PassAudit")
    public Audit PassAudit(Integer id, HttpServletRequest request) {
        //审核申请表
        Audit audit = this.auditService.queryById(id);
        audit.setAuditstate("1");
        //用户信息表
        int uid = audit.getUid();
        Userinfo userinfo = this.userinfoService.queryById(uid);
        if (audit.getDetails().equals("商家身份审核")||audit.getDetails().equals("商家身份复核"))
            userinfo.setIsseller(2);
        else
        {
            userinfo.setStucardurl(audit.getAuditimg());
            userinfo.setIscert(2);
        }
        this.userinfoService.update(userinfo);
        //写日志
        //(String operator, Date createtime, String createip, String details, String note)
        logsinfoService.insert(new Logsinfo(userinfo.getMail(), TimeUtils.NowTimeN(), new getUserIp().getIp(request), audit.getDetails(), "审核通过"));

        return this.auditService.update(audit);
    }

    /**
     * 管理员驳回申请，状态改为0，申请未通过
     *
     * @param id
     * @return
     */
    @GetMapping("ReMoveAudit")
    public Audit ReMoveAudit(Integer id,HttpServletRequest request) {
        Audit audit = this.auditService.queryById(id);
        audit.setAuditstate("0");
        //用户信息表
        int uid = audit.getUid();
        Userinfo userinfo = this.userinfoService.queryById(uid);
        if (audit.getDetails().equals("商家身份审核")||audit.getDetails().equals("商家身份复核"))
            userinfo.setIsseller(3);
        else
        {
            userinfo.setIscert(3);
        }
        this.userinfoService.update(userinfo);
        //写日志
        //(String operator, Date createtime, String createip, String details, String note)
        logsinfoService.insert(new Logsinfo(userinfo.getMail(), TimeUtils.NowTimeN(), new getUserIp().getIp(request), audit.getDetails(), "审核被驳回"));
        return this.auditService.update(audit);
    }

    /**
     * 联表查询所有对应的用户
     *
     * @return
     */
    @GetMapping("selectAll")
    public List<HashMap<String, Object>> selectAll() {
        return this.leagueTableQueryService.selectUserAudit();
    }

    /**
     * 分页联表查询
     *
     * @param map
     * @return
     */
    @PostMapping("selectKeyByLimit")
    public ResultPage selectKeyByLimit(@RequestBody HashMap<String, String> map) {
        ResultPage resultPage = new ResultPage();
        if (map.get("key").equals("")) {
            int pageTotal = this.leagueTableQueryService.queryCountUserAudit();
            List<HashMap<String, Object>> list = this.leagueTableQueryService.queryAllByLimitUserAudit(Integer.valueOf(map.get("offset")), Integer.valueOf(map.get("limit")));
            resultPage.setData(list);
            resultPage.setPageTotal(pageTotal);
        } else {
            int pageTotal = this.leagueTableQueryService.queryCountSelectUserAudit(map.get("key"));
            List<HashMap<String, Object>> list = this.leagueTableQueryService.queryKeyByLimitUserAudit(map.get("key"), Integer.valueOf(map.get("offset")), Integer.valueOf(map.get("limit")));
            resultPage.setData(list);
            resultPage.setPageTotal(pageTotal);
        }
        return resultPage;
    }


}